package com.deyuanyun.pic.pipe.service.impl;

import com.deyuanyun.pic.cache.SessionCacheSupport2;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.ConvertUtil;
import com.deyuanyun.pic.common.util.MyCollectionUtil;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.controller.dto.UserSessionVO;
import com.deyuanyun.pic.pipe.domain.PicPipeTrackPoint;
import com.deyuanyun.pic.pipe.domain.PicPipeTrackPointG;
import com.deyuanyun.pic.pipe.domain.PicPipeTrackPointGExample;
import com.deyuanyun.pic.pipe.domain.custom.CustomPicPipeTrackPoint;
import com.deyuanyun.pic.pipe.domain.exceldomain.CrossingOverInfoVO;
import com.deyuanyun.pic.pipe.domain.exceldomain.TrackPointExcelImportVO;
import com.deyuanyun.pic.pipe.mapper.CustomPicPipeTrackPointMapper;
import com.deyuanyun.pic.pipe.mapper.PicPipeTrackPointGMapper;
import com.deyuanyun.pic.pipe.mapper.PicPipeTrackPointMapper;
import com.deyuanyun.pic.support.biz.CodeUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by xu on 2016/1/22.
 * 定标点 跟踪点
 */
@Service
public class PipeTrackPointServiceImpl implements com.deyuanyun.pic.pipe.service.PipeTrackPointService {
    static Logger logger=Logger.getLogger(PipeTrackPointServiceImpl.class);

    @Autowired
    private CustomPicPipeTrackPointMapper customPicPipeTrackPointMapper;
    @Autowired
    private PicPipeTrackPointMapper picPipeTrackPointMapper;
    @Autowired
    private PicPipeTrackPointGMapper picPipeTrackPointGMapper;


    @Override
    public void delTrackPoint(String id){
        int i= picPipeTrackPointMapper.deleteByPrimaryKey(id);
        Asserts.assertTrue(i>0,"该记录已经不存在");
        PicPipeTrackPointGExample gExample=new PicPipeTrackPointGExample();
        gExample.createCriteria().andPipeTrackPointIdEqualTo(id);
        picPipeTrackPointGMapper.deleteByExample(gExample);
    }

    @Override
    public List<CustomPicPipeTrackPoint> queryTrackPointList(Map map)throws Exception{
        List<CustomPicPipeTrackPoint> customPicPipeTrackPoints=customPicPipeTrackPointMapper.queryTrackPointList(map);
        Object isConvert=map.get("isConvert");
        if (isConvert!=null && "false".equals(isConvert)){

        }else {
            CodeUtil.convertId2CodeName(customPicPipeTrackPoints,new String[]{"type"});
        }

        return customPicPipeTrackPoints;
    }
    @Override
    public void insertTrackPoint(List<TrackPointExcelImportVO> trackPointExcelImportVOList)throws Exception{
        List<PicPipeTrackPoint> identifications= ConvertUtil.transform(trackPointExcelImportVOList,PicPipeTrackPoint.class);
        Date crDaye=new Date();
        for (PicPipeTrackPoint iden:identifications) {
            String id= iden.getId();
            if (StringUtils.isBlank(id)){
                iden.setId(CommonUtil.getUUID());
            }
            iden.setIsNewest((short)0);
            iden.setDeleted((short)0);
            iden.setCreateDate(crDaye);
            UserSessionVO userSessionVO= SessionCacheSupport2.getSessionVO();
            iden.setCreateUser(userSessionVO.getUsername());
            Map map=new HashMap();
            map.put("priUuid",iden.getId());
            List<TrackPointExcelImportVO> importVOs= MyCollectionUtil.filterListByMap(trackPointExcelImportVOList,map);
            Asserts.assertTrue(ObjectUtil.isNotNullEmpty(importVOs),"导入出错了，没有找到对应的坐标记录");
            TrackPointExcelImportVO importVO=importVOs.get(0);

            PicPipeTrackPointG g=new PicPipeTrackPointG();
            g.setDeleted((short)0);
            g.setIsNewest((short)0);
            g.setConvertType("BD");
            g.setX(new BigDecimal(importVO.getLaocationX()));
            g.setY(new BigDecimal(importVO.getLaocationY()));
            g.setZ(new BigDecimal(importVO.getLaocationZ()));
            g.setRecorder(iden.getRecorder());
            g.setRecordtime(iden.getRecordtime());
            g.setPipeTrackPointId(iden.getId());
            g.setId(CommonUtil.getUUID());
            g.setDataStated("20");
            iden.setDataStated("20");
            picPipeTrackPointGMapper.insert(g);
            picPipeTrackPointMapper.insert(iden);

        }
    }

    @Override
    public List<List<String[]>> exportExcel(Map map) throws Exception {
        List<LinkedHashMap<String, Object>> data = customPicPipeTrackPointMapper.exportExcel(map);

        for(LinkedHashMap<String, Object> m : data) {
            m.put("TYPE", CodeUtil.getName((String)m.get("TYPE")));
        }

        List<List<String[]>> result=new ArrayList<List<String[]>>();
        List<String[]> lsr = JXL.toList(data, "yyyy-MM-dd");
        result.add(lsr);
        return result;
    }
}
